﻿// -----------------------------------------------------------------------
// <copyright file="orderStat.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ImageP
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    /// 
    public struct bitmixingStruct
    {
       public double red;
       public double green;
       public double blue;
       public string bitmixingValue;

    }

    public class orderStat
    {
        public Bitmap minFilter(Bitmap img,int maskSize)
        {
         
            List<bitmixingStruct> newli = new List<bitmixingStruct>();
            List<bitmixingStruct> bitmixList = new List<bitmixingStruct>();
            RGBCOLORS[,] imageBuffer = new RGBCOLORS[img.Height + 1, img.Width + 1];
            RGBCOLORS[,] maskBuffer = new RGBCOLORS[maskSize, maskSize];
            RGBCOLORS[,] finalImage = new RGBCOLORS[img.Height, img.Width];
            RGBCOLORS[,] norm = new RGBCOLORS[img.Height, img.Width];
            Bitmap finalbm = new Bitmap(img.Width,img.Height);
            int counterI = 0;
            int counterJ = 0;
            for (int i = 0; i < img.Width + 1; i++)
            {
                imageBuffer[0, i].R = 0;
                imageBuffer[img.Height, i].R = 0;

                imageBuffer[0, i].G= 0;
                imageBuffer[img.Height, i].G = 0;

                imageBuffer[0, i].B = 0;
                imageBuffer[img.Height, i].B = 0;
            }

            for (int i = 0; i < img.Height + 1; i++)
            {
                imageBuffer[i, 0].R = 0;
                imageBuffer[i, img.Width].R = 0;

                imageBuffer[i, 0].G = 0;
                imageBuffer[i, img.Width].G = 0;

                imageBuffer[i, 0].B = 0;
                imageBuffer[i, img.Width].B = 0;
            }

            for (int i = 1; i <= img.Height; i++)
            {
                for (int j = 1; j <= img.Width; j++)
                {
                    imageBuffer[i, j].R = img.GetPixel(j - 1, i - 1).R;
                    imageBuffer[i, j].G = img.GetPixel(j - 1, i - 1).G;
                    imageBuffer[i, j].B = img.GetPixel(j - 1, i - 1).B;
                }
            }
            //conv
            bitmixingStruct bitmixObj = new bitmixingStruct();
            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
                    for (int h = i; h <= (i + maskSize - 1); h++)
                    {
                        Color cl;
                        if (h == img.Height)
                        {
                            break;
                        }

                        for (int g = j; g <= (j + maskSize - 1); g++)
                        {
                            cl = img.GetPixel(j, i);
                            if (g == img.Width)
                            {
                                break;
                            }

                            maskBuffer[counterI, counterJ].R =  imageBuffer[h, g].R;
                            maskBuffer[counterI, counterJ].G =  imageBuffer[h, g].G;
                            maskBuffer[counterI, counterJ].B =  imageBuffer[h, g].B;
                           
                            counterJ++;
                        }

                        counterI++;
                        counterJ = 0;
                    }

                    counterI = 0;
                    counterJ = 0;

                    string[] bitMixingArray = new string[maskSize * maskSize];
                    int index = 0;
                    string finalRed, finalGreen, finalBlue;
                    string concatedString = "";
                    List<string> bitmixingValueList = new List<string>();

                    // bitmixing array
                    bitMixingArray.Initialize();
                    for (int a = 0; a < maskSize; a++)
                    {
                        for (int b = 0; b < maskSize; b++)
                        {
                            string red = Convert.ToString((int)maskBuffer[a, b].R, 2);
                            string green = Convert.ToString((int)maskBuffer[a, b].G, 2);
                            string blue = Convert.ToString((int)maskBuffer[a, b].B, 2);

                            bitmixObj.red = maskBuffer[a, b].R;
                            bitmixObj.green = maskBuffer[a, b].G;
                            bitmixObj.blue = maskBuffer[a, b].B;
                           

                            if (red.Length < 8)
                            {
                                int diff = 8 - red.Length;
                                concatedString = "";
                                for (int difference = 0; difference < diff; difference++)
                                {
                                    concatedString += "0";
                                }
                                finalRed = concatedString + red;
                            }
                            else
                                finalRed = red;
                            if (green.Length < 8)
                            {
                                concatedString = "";
                                int diff = 8 - green.Length;
                                for (int difference = 0; difference < diff; difference++)
                                {
                                    concatedString += "0";
                                }
                                finalGreen = concatedString + green;
                            }
                            else
                                finalGreen = green;

                            if (blue.Length < 8)
                            {
                                concatedString = "";
                                int diff = 8 - blue.Length;
                                for (int difference = 0; difference < diff; difference++)
                                {
                                    concatedString += "0";
                                }
                                finalBlue = concatedString + blue;
                            }
                            else
                                finalBlue = blue;
                            for (int c = 0; c < 8; c++)
                            {

                                bitMixingArray[index] += (finalRed[c].ToString() + finalGreen[c].ToString() + finalBlue[c].ToString());

                            }
                            long l = Convert.ToInt64(bitMixingArray[index] , 2);
                            bitmixObj.bitmixingValue = Convert.ToString(l);
                            bitmixList.Add(bitmixObj);
                            
                            index++;
                        }//mask size
                    }//mask size
                 //   bitmixList.Sort(delegate(bitmixingStruct b1, bitmixingStruct b2) {return b1.bitmixingValue.CompareTo(b2.bitmixingValue); });
                   
                     bitmixList.Sort((x,y) => string.Compare(x.bitmixingValue,y.bitmixingValue));

                     finalImage[i, j].R = bitmixList[0].red;
                     finalImage[i, j].G = bitmixList[0].green;
                     finalImage[i, j].B = bitmixList[0].blue;
               

                    bitmixList.Clear();
                }//image width
            }//image height
            for (int i = 0; i < img.Height; i++)
            {
                for (int j = 0; j < img.Width; j++)
                {
               
                    finalbm.SetPixel(j, i, System.Drawing.Color.FromArgb(Convert.ToInt32(finalImage[i, j].R), Convert.ToInt32(finalImage[i, j].G), Convert.ToInt32(finalImage[i, j].B)));
                }
            }
            return finalbm;
        }
    }
}
